package space.sakana.edusysback.service;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import space.sakana.edusysback.entity.Admin;
import space.sakana.edusysback.entity.Teacher;
import space.sakana.edusysback.entity.User;
import space.sakana.edusysback.mapper.AdminMapper;
import space.sakana.edusysback.mapper.TeacherMapper;
import space.sakana.edusysback.result.DataResult;
import space.sakana.edusysback.result.Result;
import space.sakana.edusysback.util.CookieUtil;

@Service
public class TeacherService {

    private TeacherMapper teacherMapper;
    private AdminMapper adminMapper;

    @Autowired
    public void autowiredMapper(TeacherMapper teacherMapper, AdminMapper adminMapper){
        this.teacherMapper = teacherMapper;
        this.adminMapper = adminMapper;
    }

    public Result getAllTeacher(HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        return new DataResult(JSON.toJSONString(teacherMapper.selectAllTeacher()));
    }

    public Result addTeacher(String data, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        return new Result(teacherMapper.insertTeacher(JSON.parseObject(data, Teacher.class)));
    }
}
